package com.css.dzj.dzjfxyj.modules.over.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjfxyj.common.util.HttpClientUtil;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.distribute.entity.JgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.over.service.OverServcie;
import com.css.dzj.dzjfxyj.modules.over.vo.OverVo;
import com.css.dzj.dzjfxyj.modules.reply.entitiy.JgFxyjFeedbackEntity;
import com.css.dzj.dzjfxyj.modules.reply.vo.ReplyVo.ReplyVo;
import com.google.gson.JsonObject;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/over")
public class OverController {
    @Autowired
    OverServcie overServcie;
    @Value("${AP_URL}")
    private String apUrl;
    @Autowired
    BaseService baseService;
    /**
     * 分页查询结束信息
     * @param pageSize
     * @param pageNo
     * @param fxyjDomainId
     * @param fxyjTitle
     * @param fxyjLevel
     * @param fxyjObjectName
     * @param startTime
     * @param endTime
     * @param startTime1
     * @param endTime1
     * @return
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                     @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                     @RequestParam(name = "fxyjDomainId" ,required = false)String fxyjDomainId,
                     @RequestParam(name = "fxyjTitle" ,required = false)String fxyjTitle,
                     @RequestParam(name = "fxyjLevel" ,required = false)String fxyjLevel,
                     @RequestParam(name = "fxyjObjectName" ,required = false)String fxyjObjectName,
                     @RequestParam(name = "startTime" ,required = false)String startTime,
                     @RequestParam(name = "endTime" ,required = false)String endTime,
                     @RequestParam(name = "startTime1" ,required = false)String startTime1,
                     @RequestParam(name = "endTime1" ,required = false)String endTime1,HttpServletRequest request){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        QueryResults<JgFxyjWarningSignalEntity> page=overServcie.getPage(pageRequest,fxyjDomainId,fxyjTitle,fxyjLevel,
                fxyjObjectName,startTime,endTime,startTime1,endTime1);
        baseService.logSuccess(3,"结束查询成功","2");
        return R.ok().put("data",page);
    }

    /***
     * 保存结果
     * @param replyVo
     * @param request
     * @return
     */
    @PostMapping("/saveOver")
    public R saveOver(@RequestBody ReplyVo replyVo, HttpServletRequest request){
        try {
           List<JgFxyjFeedbackEntity>list=replyVo.getRiskwarningChildren();
            for (JgFxyjFeedbackEntity jgFxyjFeedbackEntity:list) {
                overServcie.saveOver(jgFxyjFeedbackEntity,replyVo.getRiskwarning().getId(), request);
            }
            baseService.logSuccess(3,"保存结束成功","2");
            return R.ok("结束成功");
        }catch (Exception e){
            baseService.logFailed(3,"保存结束失败","2");
            return R.error(e.getMessage());
        }
    }

    /***
     * 查看详情
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public R findById(@RequestParam String id){
        if(!StringUtils.isEmpty(id)) {
            JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity = overServcie.findById(id);
            List<JgFxyjFenfaEntity> jgFxyjFenfaEntity = overServcie.findDistrById(id);
            JgFxyjFeedbackEntity jgFxyjFeedbackEntity = overServcie.findFeebackById(jgFxyjFenfaEntity.get(0).getId());
           ReplyVo overVo= new ReplyVo();
            List<JgFxyjFeedbackEntity> list=new ArrayList<>();
            list.add(jgFxyjFeedbackEntity);
            overVo.setRiskwarning(jgFxyjWarningSignalEntity);
            overVo.setRiskwarningChildren(list);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data",overVo);
        }else {
            baseService.logFailed(3,"查询失败","2");
            return R.error("id不能为空");
        }
    }

    /**
     * 去安评里调接口
     * @param code
     * @return
     */
    @GetMapping("/getAction")
    public R getAction(@RequestParam String code){
        String msg = HttpClientUtil.doGet(apUrl+"/check/getAction?code="+code);
        JSONObject jsonObject = JSON.parseObject(msg);
        Object data = jsonObject.get("data");
        if (StringUtils.isEmpty(data)){
            return R.error("没有对应数据！");
        }
        baseService.logSuccess(3,"查询行政行为成功","2");
        return R.ok().put("data",data);
    }
}
